<?php

namespace App\Http\Controllers\TodoList;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cache;
use DB;
use App\Basket;
use App\BasketLog;
use App\Setting;
use App\Bid;
use App\Contract;
use App\Demand;
use App\Offer;
use App\Enquiry;
use App\Company;

class TodoListController extends Controller
{

    public function welcome()
    {
        //return 'Welcome!';
        $time = time();
        $date = date('Y-m-d', $time);
        $current_time = date('H:i:s', $time);
        $datetime = date('Y-m-d H:i:s', $time);
        $current_user = auth()->user();
        $basket_name = $date;

        $role_id = $current_user->role_id;
        $company_id = $current_user->company_id;

        // 取得角色权限 数组
        $features = Cache::get('permission.role.' . auth()->user()->role->id);

        //待报价的报价单
        $enquiries = [];
        if (in_array('enquiry.supplier.reply', $features)) {
            $current_supplier = $current_user->supplier;
            $supply_goods = json_decode($current_supplier->goods, true);
            $enquiries = Enquiry::whereIn('goods_id', $supply_goods);
            $enquiries = $enquiries->orderBy('created_at', 'desc');
            $enquiries = $enquiries->where('stop_at', '>', $datetime);
            $enquiries = $enquiries->where('start_at', '<=', $datetime);
            $enquiries = $enquiries->whereHas('replies', function($query) use($current_user) {
                return $query->select(DB::raw("CASE WHEN count(*) = 0 THEN 1 ELSE 0 END"))->where('supplier_id', $current_user->id);
            });
            $enquiries = $enquiries->take(5)->get();
        }

        //待报价的标书
        $bids = [];
        if (in_array('bid.supplier.offer', $features)) {
            $current_supplier = $current_user->supplier;
            $supply_goods = json_decode($current_supplier->goods, true);
            $bids = Bid::orderBy('created_at', 'desc')
                ->whereIn('goods_id', $supply_goods)
                ->where('offer_start', '<=', $datetime)
                ->where('offer_stop', '>=', $datetime)
                ->whereHas('basket', function ($query) use ($datetime) {
                    return $query->where('state', 'bided')->where('bided_at', '<=', $datetime);
                });

            $bids = $bids->whereHas('demands', function($query) {
                return $query->select(DB::raw('CASE WHEN sum("quantity") > 0 THEN 1 ELSE 0 END'))->groupBy('bid_id');
            });

            $bids = $bids->where(function ($query) use ($supply_goods, $current_user) {
                return $query->where(function ($query) use ($supply_goods) {
                    return $query->where('type', 'global')->whereIn('goods_id', $supply_goods);
                })->orWhere(function ($query) use ($current_user) {
                    return $query->where('type', 'invite')->whereRaw('\'["' . $current_user->id . '"]\'::jsonb <@ "suppliers"');
                });
            });

            $bids = $bids->get();
        }

        return view('dashboard.wechat_welcome')
            ->with('bids', $bids)
            ->with('tab_active', 'dashboard')
            ->with('enquiries', $enquiries);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
